//
//  InfoViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "InfoViewController.h"
#import "MyselfInfoTableViewCell.h"
#import "UpdateNameViewController.h"
#import "PersonExViewController.h"

@interface InfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
    
    UIImagePickerController *_imagePickerController;
}

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * showStringArray;
@property (nonatomic, strong)UIAlertController * alertVC;
@property (nonatomic, strong)NSIndexPath * index;

@end

@implementation InfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    
    self.showStringArray = @[@"头像",@"名字",@"个性签名"];
    
    [self setupNav];
    [self setupUI];
    [self setupAlertVC];
}

- (void)setupNav{
    
    self.title = @"个人信息";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
//    tableView.rowHeight = 45 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyselfInfoTableViewCell class] forCellReuseIdentifier:@"MyselfInfoListCell"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyselfInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyselfInfoListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.showStringArray[indexPath.row];
    
    if(indexPath.row == 0){
        
        cell.iconImageView.hidden = NO;
        [cell.iconImageView sd_setImageWithURL:self.url placeholderImage:ImageNamed(@"weidenglu")];
        
        cell.subLabel.hidden = YES;
        self.index = indexPath;
    }else{
        
        cell.iconImageView.hidden = YES;
        cell.subLabel.hidden = NO;
        if (indexPath.row == 1) {
            
            cell.subLabel.text = self.name;
        }else{
            
            cell.subLabel.text = self.person;
        }
    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        
        return 60 * SCREENH6;
    }else{
        
        return 40 * SCREENH6;
    }
}



- (void)setupAlertVC{
    
    self.alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"选择照片方式" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * camerAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * albumsAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [self.alertVC addAction:camerAction];
    [self.alertVC addAction:albumsAction];
    [self.alertVC addAction:cancelAction];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo

{
    
    //    [self.pictureButton setImage:image forState:UIControlStateNormal];
    
    MyselfInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:self.index];
    
    cell.iconImageView.image = image;
    
    NSData *imageData = UIImageJPEGRepresentation(image,0.5);;

    [[ZSNetworkTool sharedTool]commitChangeMyIconWithfiles:imageData resultWithBlock:^(BOOL success, id responseData) {
        if(success){

            NSLog(@"====成功了");

        }else{

        }
    }];

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//进入拍摄页面点击取消按钮

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if (indexPath.row == 0) {
        
        self.index = indexPath;
        
        [self presentViewController:self.alertVC animated:YES completion:nil];
    }else if (indexPath.row == 1){
        
        UpdateNameViewController * updateNameVC = [UpdateNameViewController new];
        
        updateNameVC.returnValueBlock = ^(NSString *strValue) {
            
            MyselfInfoTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
            
            cell.subLabel.text = strValue;
            self.name = strValue;
            
            [self.tableView reloadData];
        };
        
        [self.navigationController pushViewController:updateNameVC animated:YES];
    }else if (indexPath.row == 2){
        
        PersonExViewController * updateNameVC = [PersonExViewController new];
        
        updateNameVC.returnValueBlock = ^(NSString *strValue) {
            
            MyselfInfoTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
            
            cell.subLabel.text = strValue;
            self.person = strValue;
            
             [self.tableView reloadData];
        };
        
        [self.navigationController pushViewController:updateNameVC animated:YES];
    }
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
